package com.util.okhttputils.callback;

import com.google.gson.Gson;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by marlon on 2016-05-07.
 */
public abstract class ObjectCallBack<T> extends Callback<T>{
   private Class<T> cls;

    public ObjectCallBack(Class<T> cls) {
        this.cls = cls;
    }

    @Override
    public T parseNetworkResponse(Response response) throws Exception {
        String string = response.body().string();
        T object=null;
        try {
            object = (T) new Gson().fromJson(string, cls);
        }catch (Exception e){
            e.printStackTrace();
        }
        return object;
    }

    @Override
    public void onError(Call call, Exception e) {
        onFail(call,e);
    }

    @Override
    public void onResponse(T response) {
        onSuccess(response);
    }
    public abstract void onSuccess(T response);
    public abstract void onFail(Call call, Exception e);
}
